using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Terra.Core.Configuration;
using Terra.Core.Infrastructure.Security;

namespace Terra.Data.Context;

/// <summary>
/// Terra数据库上下文工厂
/// </summary>
public class TerraDbContextFactory : IDesignTimeDbContextFactory<TerraDbContext>
{
    /// <summary>
    /// 创建数据库上下文
    /// </summary>
    public TerraDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<TerraDbContext>();
        optionsBuilder.UseSqlServer("Server=127.0.0.1;Database=TerraDb;User Id=sa;Password=123456;TrustServerCertificate=True;MultipleActiveResultSets=true");

        return new TerraDbContext(
            optionsBuilder.Options,
            new ProjectSettings { TablePrefix = "Terra_" },
            new DesignTimeCurrentUser());
    }
}

/// <summary>
/// 设计时当前用户实现
/// </summary>
public class DesignTimeCurrentUser : ICurrentUser
{
    public string Name => "DesignTime";
    public Guid? Id => Guid.Empty;
    public string[] Roles => Array.Empty<string>();
    public Guid? TenantId => null;
    public bool IsAuthenticated => false;
}